package com.yh.controller;

import com.yh.service.IExcelService;
import lombok.AllArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author yh
 * @date 2021-4-4
 */
@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileController {
    private final IExcelService excelService;

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = excelService.exportExcel();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + "用户信息" + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        workbook.close();

    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestParam(required = false,value = "file") MultipartFile file) {
        excelService.importExcel(file);
    }
}
